package com.ruibang.glass.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.domain.req.MaterialCheckManagementReq;
import com.ruibang.glass.material.entity.MaterialCheckItem;
import com.ruibang.glass.material.entity.MaterialManagement;
import com.ruibang.glass.material.entity.MaterialWarehouseInventoryCheck;
import com.ruibang.glass.material.mapper.MaterialCheckItemMapper;
import com.ruibang.glass.material.mapper.MaterialWarehouseInventoryCheckMapper;
import com.ruibang.glass.material.service.GMaterialManagementService;
import com.ruibang.glass.material.service.MaterialWarehouseInventoryCheckService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 仓库盘点 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-02
 */
@Service
public class GMaterialWarehouseInventoryCheckServiceImpl extends ServiceImpl<MaterialWarehouseInventoryCheckMapper, MaterialWarehouseInventoryCheck> implements MaterialWarehouseInventoryCheckService {
    @Autowired
    private GMaterialManagementService materialManagementService;

    @Autowired
    private MaterialCheckItemMapper materialCheckItemMapper;


    /**
     * 仓库盘点 服务实现类
     *
     * @param
     * @return
     */

    @Override
    public IPage<MaterialWarehouseInventoryCheck> getPage(MaterialCheckManagementReq warehouseInfo) {
        LambdaQueryWrapper<MaterialWarehouseInventoryCheck> queryWrapper = Wrappers.<MaterialWarehouseInventoryCheck>lambdaQuery();
        if (StringUtils.isNotBlank(warehouseInfo.getCheckerId())) {
            queryWrapper.eq(MaterialWarehouseInventoryCheck::getCheckerId, warehouseInfo.getCheckerId());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getInventoryCheckNo())) {
            queryWrapper.like(MaterialWarehouseInventoryCheck::getInventoryCheckNo, warehouseInfo.getInventoryCheckNo());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getStatus())) {
            queryWrapper.eq(MaterialWarehouseInventoryCheck::getStatus, warehouseInfo.getStatus());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getWarehouseName())) {
            queryWrapper.eq(MaterialWarehouseInventoryCheck::getWarehouseName, warehouseInfo.getWarehouseName());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getLoginUserId())) {
            queryWrapper.eq(MaterialWarehouseInventoryCheck::getCheckerId, warehouseInfo.getLoginUserId());
        }
        Page<MaterialWarehouseInventoryCheck> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(warehouseInfo.getPageNo(), warehouseInfo.getPageSize()),
                queryWrapper.orderByDesc(MaterialWarehouseInventoryCheck::getCreateTime)
        );
        return materialWarehouseInfoPage;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    @Override
    public MaterialWarehouseInventoryCheck getMaterialWarehouseInventoryCheck(String id) {
        MaterialWarehouseInventoryCheck materialWarehouseInventoryCheck = baseMapper.selectById(id);
        return materialWarehouseInventoryCheck;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCheckItem(MaterialWarehouseInventoryCheck materialWarehouseInventoryCheck) {
        MaterialWarehouseInventoryCheck materialWarehouseInventoryCheck1 = baseMapper.selectById(materialWarehouseInventoryCheck.getId());
        materialWarehouseInventoryCheck1.setStatus(Constants.TWO_STR);
        baseMapper.updateById(materialWarehouseInventoryCheck1);
        if (CollectionUtils.isNotEmpty(materialWarehouseInventoryCheck.getMaterialCheckItemList())) {
            materialCheckItemMapper.delete(Wrappers.<MaterialCheckItem>lambdaQuery().eq(MaterialCheckItem::getCheckId, materialWarehouseInventoryCheck.getId()));
            for (MaterialCheckItem materialCheckItem : materialWarehouseInventoryCheck.getMaterialCheckItemList()) {
                materialCheckItem.setId("");
                materialCheckItem.setCheckId(materialWarehouseInventoryCheck.getId());
                materialCheckItemMapper.insert(materialCheckItem);
            }
        }
    }
}
